package com.zkyc.framework.website.controller;

import com.zkyc.framework.common.Enum.LogType;
import com.zkyc.framework.common.annotation.Log;
import com.zkyc.framework.common.exception.ResultCode;
import com.zkyc.framework.common.pojo.Website;
import com.zkyc.framework.common.vo.ResultVO;
import com.zkyc.framework.common.vo.website.WebsitePageVo;
import com.zkyc.framework.website.service.WebsiteService;
import com.zkyc.framework.website.vo.UrlAndRightVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * (Website)表控制层
 *
 * @author libingchuan
 * @since 2022-05-23 16:22:05
 */
@Slf4j
@RestController
@RequestMapping
public class WebsiteController {
    /**
     * 服务对象
     */
    @Resource
    private WebsiteService websiteService;

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @GetMapping("listByPage")
    public ResultVO queryByPage(WebsitePageVo websitePageVo) {
        return websiteService.queryByPage(websitePageVo);
    }

    /**
     * 查询所有站点
     *
     * @return 查询结果
     */
    @GetMapping("list")
    public ResultVO queryList() {
        return websiteService.queryList();
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/query/{id}")
    public ResultVO queryById(@PathVariable("id") Integer id) {
        Website website = websiteService.queryById(id);
        if (website == null) {
            return ResultVO.fail(ResultCode.SERVE_ERROR);
        }
        return ResultVO.data(website);
    }

    /**
     * 新增数据
     *
     * @param website 实体
     * @return 新增结果
     */
    @PostMapping("insert")
    @Log(type = LogType.新建站点)
    public ResultVO save(@RequestBody Website website) throws Exception {
        return websiteService.save(website);
    }

    /**
     * 编辑数据
     *
     * @param website 实体
     * @return 编辑结果
     */
    @PostMapping("edit")
    @Log(type = LogType.编辑站点)
    public ResultVO edit(@RequestBody Website website) {
        return websiteService.update(website);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @GetMapping("delete")
    public ResultVO deleteById(Integer id) {
        return websiteService.deleteById(id);
    }

    /**
     * 查询网站的域名列表
     *
     * @return
     */
    @GetMapping("queryUrlList")
    public ResultVO queryUrlList() {
        return websiteService.queryUrlList();
    }


    @GetMapping("insertViews")
    public void insertViews(Integer webSiteId) {
        websiteService.insertViews(webSiteId);
    }

    /**
     * 网站的禁用与启用
     *
     * @param websiteId
     * @param type
     * @return
     */
    @GetMapping("forbiddenWebsite")
    @Log(type = LogType.启用禁用站点)
    public ResultVO forbiddenWebsite(@RequestParam Integer websiteId, @RequestParam Integer type) {
        return websiteService.forbiddenWebsite(websiteId, type);
    }

    /**
     * 通过域名查询网站
     *
     * @param url
     * @return
     */
    @GetMapping("queryWebsiteIdByUrl")
    public ResultVO getWebsiteIdByUrl(@RequestParam String url) {
        return websiteService.getWebsiteIdByUrl(url);
    }


    @GetMapping("queryLink")
    public ResultVO queryLink() {
        return websiteService.queryLink();
    }

    @GetMapping("queryTalent")
    public ResultVO queryTalent() {
        return websiteService.queryTalent();
    }

    /**
     * 新增网站时设置网站的域名和权限
     *
     * @param urlAndRightVo
     * @return
     */
    @PostMapping("setUrlAndRight")
    public ResultVO setUrlAndRight(@RequestBody UrlAndRightVo urlAndRightVo) {
        return websiteService.setUrlAndRight(urlAndRightVo);
    }
    @GetMapping("webChangeGray")
    public ResultVO webChangeGray(Integer websiteId,Integer changeGray ) {
        return websiteService.webChangeGray(websiteId,changeGray);
    }

    @GetMapping("saveRightByTemplateId")
    public ResultVO saveRightByTemplateId(String  webUrl,Integer templateId ) {
        return websiteService.saveRightByTemplateId(webUrl,templateId);
    }


    @PostMapping("deploy")
    public ResultVO deploy(@RequestBody Website website) {
        return websiteService.deploy(website);
    }


}

